package com.lxmes.hr.service.impl;

import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcAbnormalMoneyMapper;
import com.lxmes.hr.domain.NcAbnormalMoney;
import com.lxmes.hr.service.INcAbnormalMoneyService;
import javax.validation.Validator;

/**
 * 工资异动审批Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-17
 */
@Service
public class NcAbnormalMoneyServiceImpl implements INcAbnormalMoneyService
{
    @Autowired
    private NcAbnormalMoneyMapper ncAbnormalMoneyMapper;

    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询工资异动审批
     *
     * @param id 工资异动审批主键
     * @return 工资异动审批
     */
    @Override
    public NcAbnormalMoney selectNcAbnormalMoneyById(Long id)
    {
        return ncAbnormalMoneyMapper.selectNcAbnormalMoneyById(id);
    }

    /**
     * 查询工资异动审批列表
     *
     * @param ncAbnormalMoney 工资异动审批
     * @return 工资异动审批
     */
    @Override
    public List<NcAbnormalMoney> selectNcAbnormalMoneyList(NcAbnormalMoney ncAbnormalMoney)
    {
        return ncAbnormalMoneyMapper.selectNcAbnormalMoneyList(ncAbnormalMoney);
    }

    /**
     * 新增工资异动审批
     *
     * @param ncAbnormalMoney 工资异动审批
     * @return 结果
     */
    @Override
    public int insertNcAbnormalMoney(NcAbnormalMoney ncAbnormalMoney)
    {
        List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
        if(!StringUtils.isNull(ncOrgManages)){
            ncAbnormalMoney.setTenantId(ncOrgManages.get(0).getJtdm());
        }
        ncAbnormalMoney.setCreateTime(DateUtils.getNowDate());
        ncAbnormalMoney.setCreateBy(SecurityUtils.getUsername());

        return ncAbnormalMoneyMapper.insertNcAbnormalMoney(ncAbnormalMoney);
    }

    /**
     * 修改工资异动审批
     *
     * @param ncAbnormalMoney 工资异动审批
     * @return 结果
     */
    @Override
    public int updateNcAbnormalMoney(NcAbnormalMoney ncAbnormalMoney)
    {
        return ncAbnormalMoneyMapper.updateNcAbnormalMoney(ncAbnormalMoney);
    }

    /**
     * 批量删除工资异动审批
     *
     * @param ids 需要删除的工资异动审批主键
     * @return 结果
     */
    @Override
    public int deleteNcAbnormalMoneyByIds(Long[] ids)
    {
        return ncAbnormalMoneyMapper.deleteNcAbnormalMoneyByIds(ids);
    }

    /**
     * 删除工资异动审批信息
     *
     * @param id 工资异动审批主键
     * @return 结果
     */
    @Override
    public int deleteNcAbnormalMoneyById(Long id)
    {
        return ncAbnormalMoneyMapper.deleteNcAbnormalMoneyById(id);
    }
    @Override
    public String imporIteminfo(List<NcAbnormalMoney> ncAbnormalMoneyList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncAbnormalMoneyList) || ncAbnormalMoneyList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcAbnormalMoney ncAbnormalMoney : ncAbnormalMoneyList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncAbnormalMoney);
                ncAbnormalMoney.setCreateBy(operName);
                ncAbnormalMoney.setUpdateBy(operName);
                this.insertNcAbnormalMoney(ncAbnormalMoney);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
